package cn.cdtu.tibetplan.controller;

import java.util.List;

import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.service.SchoolService;
import cn.cdtu.tibetplan.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.cdtu.tibetplan.service.DemandService;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private DemandService demandService;
    @Autowired
    private SchoolService schoolService;
    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<Demand> findAll() {
        return demandService.findAll();
    }

    @RequestMapping("/list")
    public String list() {
        return "redirect:search?currentPage=1&pageSize=10";
    }

    @RequestMapping("/search")
    public String search(Demand demand, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = demandService.findPage(demand, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "/Need/index";
    }

    @RequestMapping("/addDemand")
    public String addDemand(Model model){
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools",schools);
        return "/Need/add";
    }

    @RequestMapping("/show")
    public String show(Long id,Model model){
        Demand demand = demandService.findOne(id.longValue());
        model.addAttribute("show",demand);
        return "/Need/show";
    }
    @RequestMapping("/edit")
    public String edit(Long id,Model model){
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools",schools);
        Demand demand = demandService.findOne(id.longValue());
        model.addAttribute("edit",demand);
        return "/Need/edit";
    }

    /**
     * 增加
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Demand demand) {
        try {
            School school = schoolService.findOne(demand.getSchoolid().longValue());
            demand.setSchoolname(school.getSchoolname());
            demand.setCreatetime(Tools.getTime());
            demand.setUpdatetime(Tools.getTime());
            demandService.add(demand);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param demand
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public Result update(Demand demand) {
        try {
            School school = schoolService.findOne(demand.getSchoolid().longValue());
            demand.setSchoolname(school.getSchoolname());
            demand.setUpdatetime(Tools.getTime());
            demandService.update(demand);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public Demand findOne(Long id) {
        return demandService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            demandService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/searchDemand")
    public String searchDemand(Demand demand,Model model){
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools",schools);
        PageResult result = demandService.findPage(demand, 1, 0);
        List<Demand> list = result.getRows();
        model.addAttribute("demands", list);
        return "/Need/index";
    }

}
